package com.stormrage.edu.dao.edu.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.edu.EduMajorDao;
import com.stormrage.entity.edu.EduMajor;
import com.stormrage.entity.learn.rep.LeaMajorStudyRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EduMajorDaoImpl.java
 * @Package com.stormrage.edu.dao.edu.impl
 * @Description: 专业 Dao
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Repository
public class EduMajorDaoImpl extends MysBaseDao implements EduMajorDao {

	private static final Logger logger = LoggerFactory.getLogger(EduProjectDaoImpl.class);

	@Override
	public Integer queryCountByProjectId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryCountByProjectId 】-【 STARTED 】");

		final String query = "SELECT COUNT(*) FROM edu_major AS a, edu_project_major AS b WHERE a.id = b.major_id AND b.project_id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryCountByProjectId 】-【 COMPLETED 】");
		return count;

	}

	@Override
	public EduMajor queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 QueryById 】-【 STARTED 】");

		final String query = "SELECT * FROM edu_major WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduMajor entity = queryObject(query, EduMajor.class, map);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 QueryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<EduMajor> queryListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 QueryListById 】-【 STARTED 】");

		final String query = "SELECT a.* FROM edu_major AS a, edu_project_major AS b WHERE a.id = b.major_id AND b.project_id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduMajor> array = queryList(query, EduMajor.class, map);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 QueryListById 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaMajorStudyRep> queryListForStudy(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryListForStudy 】-【 STARTED 】");

		final String query = "SELECT d.id, a.major, a.module, b.locked, d.study_date_start, d.study_date_end"
				+ " FROM edu_major AS a, lea_student AS b, lea_bunch AS c, lea_term AS d"
				+ " WHERE a.id = d.major_id AND NOW() > d.study_date_start AND NOW() < d.study_date_end AND"
				+ " d.id = c.term_id AND c.id = b.bunch_id AND b.acct_id = :acctId";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaMajorStudyRep> array = queryList(query, LeaMajorStudyRep.class, map);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryListForStudy 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaMajorStudyRep> queryListForExam(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryListForStudy 】-【 STARTED 】");

		final String query = "SELECT a.id, a.major, a.module, d.id AS termId, d.exam_date_start, d.exam_date_end"
				+ " FROM edu_major AS a, lea_student AS b, lea_bunch AS c, lea_term AS d WHERE a.id = d.major_id"
				+ " AND NOW() > d.exam_date_start  AND NOW() < d.exam_date_end AND d.id = c.term_id AND c.id = b.bunch_id"
				+ " AND b.acct_id = :acctId";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaMajorStudyRep> array = queryList(query, LeaMajorStudyRep.class, map);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryListForStudy 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaMajorStudyRep> queryListForSelect(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryListForStudy 】-【 STARTED 】");

		final String query = "SELECT a.id, a.major, a.module, d.id AS termId, d.study_date_start, d.study_date_end"
				+ " FROM edu_major AS a, lea_student AS b, lea_bunch AS c, lea_term AS d, lea_student_select AS e"
				+ " WHERE a.id = d.major_id  AND a.module = 0 AND NOW() > d.apply_date_start AND NOW() < d.exam_date_end"
				+ " AND d.id = c.term_id AND c.id = b.bunch_id AND b.id = e.stu_id AND e.selected = :selected"
				+ " AND b.acct_id = :acctId";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaMajorStudyRep> array = queryList(query, LeaMajorStudyRep.class, map);

		logger.debug(" 【 Dao 】-【 EduMajorDao 】-【 queryListForStudy 】-【 COMPLETED 】");
		return array;

	}

}
